﻿/*
*┌────────────────────────────────────────────────┐
*│　描    述：DBConvert                                                   
*│　作    者： (Jxw)                                             
*│　版    本：1.0     
*│　功能描述：                                          
*│　创建时间：2021/6/10 16:47:11                        
*└────────────────────────────────────────────────┘
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Utility
{
    public class DBConvert
    {
        /// <summary>
        /// 将时间范型转为数据库类型，处理空值
        /// </summary>
        /// <param name="arg">可为空时间范型</param>
        /// <returns>数据库类型</returns>
        public static object DateTimeNullToDBType(Nullable<DateTime> arg)
        {
            return arg.HasValue ? (object)arg.Value : (object)DBNull.Value;
        }

        /// <summary>
        /// 将数据库类型转为时间范型，处理空值
        /// </summary>
        /// <param name="arg">数据库值</param>
        /// <returns>时间范型</returns>
        public static Nullable<DateTime> DBTypeToDateTime(object arg)
        {
            return arg.Equals(DBNull.Value) ? (Nullable<DateTime>)null : (Nullable<DateTime>)arg;
        }

        /// <summary>
        /// 将数据库类型转为字符串，处理空值
        /// </summary>
        /// <param name="arg"></param>
        /// <returns></returns>
        public static string DBTypeToString(object arg)
        {
            return arg.Equals(DBNull.Value) ? null : (string)arg;
        }

        /// <summary>
        /// 将数据库类型转为单精度浮点型，处理空值
        /// </summary>
        /// <param name="arg"></param>
        /// <returns></returns>
        public static Nullable<float> DBTypeToFloat(object arg)
        {
            return arg.Equals(DBNull.Value) ? (Nullable<float>)null : (Nullable<float>)arg;
        }

        /// <summary>
        /// 将数据库类型转为双精度浮点型，处理空值
        /// </summary>
        /// <param name="arg"></param>
        /// <returns></returns>
        public static Nullable<double> DBTypeToDouble(object arg)
        {
            return arg.Equals(DBNull.Value) ? (Nullable<double>)null : (Nullable<double>)arg;
        }

        /// <summary>
        /// 将数据库类型转为BOOL值，处理空值
        /// </summary>
        /// <param name="arg"></param>
        /// <returns></returns>
        public static Nullable<bool> DBTypeToBool(object arg)
        {
            return arg.Equals(DBNull.Value) ? (Nullable<bool>)null : (Nullable<bool>)arg;
        }

        /// <summary>
        /// 将数据库类型转为int，处理空值
        /// </summary>
        /// <param name="arg"></param>
        /// <returns></returns>
        public static Nullable<int> DBTypeToInt(object arg)
        {
            return arg.Equals(DBNull.Value) ? (Nullable<int>)null : (Nullable<int>)arg;
        }

        /// <summary>
        /// 将可为空的字符串转换为数据库类型
        /// </summary>
        /// <param name="arg"></param>
        /// <returns></returns>
        public static object StringNullToDBType(string arg)
        {
            return (arg != null) ? (object)arg : (object)DBNull.Value;
        }

        /// <summary>
        /// 将可为空的整型转化为数据库类型
        /// </summary>
        /// <param name="arg"></param>
        /// <returns></returns>
        public static object IntNullToDBType(Nullable<int> arg)
        {
            return arg.HasValue ? (object)arg.Value : (object)DBNull.Value;
        }

        /// <summary>
        /// 将可为空的单精度浮点型转化为数据库类型
        /// </summary>
        /// <param name="arg"></param>
        /// <returns></returns>
        public static object FloatNullToDBType(Nullable<float> arg)
        {
            return arg.HasValue ? (object)arg.Value : (object)DBNull.Value;
        }

        /// <summary>
        /// 将可为空的双精度浮点型转化为数据库类型
        /// </summary>
        /// <param name="arg"></param>
        /// <returns></returns>
        public static object DoubleNullToDBType(Nullable<double> arg)
        {
            return arg.HasValue ? (object)arg.Value : (object)DBNull.Value;
        }

        /// <summary>
        /// 将Null字符串转换为""
        /// </summary>
        /// <param name="arg"></param>
        /// <returns></returns>
        public static string StringNullToEmpty(string arg)
        {
            return (arg != null) ? arg : "";
        }
    }
}
